﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Models;
using MethodWorx.CMS.UI.Application;
using MethodWorx.CMS.UI.Application.ActionResults;
using MethodWorx.Core.DomainModel;
using MethodWorx.Core.Validation;

namespace MethodWorx.CMS.UI.Controllers
{
    public class WorkflowController : ApplicationController
    {
        private IModelFactory<CreateWorkflowModel> createWorkflowModelFactory;
        private IModelFactory<WorkflowSettingsModel> workflowSettingsModelFactory;
        //
        // GET: /Workflow/
        public WorkflowController(IModelFactory<CreateWorkflowModel> createWorkflowModelFactory, IModelFactory<WorkflowSettingsModel> workflowSettingsModelFactory)
        {
            this.createWorkflowModelFactory = createWorkflowModelFactory;
            this.workflowSettingsModelFactory = workflowSettingsModelFactory;
        }

        [HttpGet]
        public ActionResult Create(Guid id)
        {
            var model = this.createWorkflowModelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id);

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Guid id, string submit, FormCollection form)
        {
            var model = this.createWorkflowModelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id);

            if (submit == "cancel")
                return new CloseDialogResult();

            this.TryUpdateModel(model.Request, "Request");

            if (this.ModelState.IsValid)
            {
                try
                {
                    model.Complete(this);
                    return new CloseDialogResult();
                }
                catch (BusinessRuleException bre)
                {
                    this.ViewData["error"] = bre.Message;
                    bre.AddToModelState(this.ModelState);
                }
            }

            return View(model);
        }


     
        
        [HttpGet]
        public ActionResult Settings(Guid id, string workflow)
        {
            var model = this.workflowSettingsModelFactory.CreateModel(this.CurrentUser);
            model.InitializeForEdit(id, workflow);

            return View(model);
        }

        [HttpPost]
        public ActionResult Settings(Guid id, string workflow, string submit, FormCollection form)
        {
            var model = this.workflowSettingsModelFactory.CreateModel(this.CurrentUser);
            model.InitializeForEdit(id, workflow);

            if (submit == "cancel")
                return new CloseDialogResult();

            this.TryUpdateModel(model.Request, "Request");

            if (this.ModelState.IsValid)
            {
                try
                {
                    model.Complete(this);
                    return new CloseDialogResult();
                }
                catch (BusinessRuleException bre)
                {
                    this.ViewData["error"] = bre.Message;
                    bre.AddToModelState(this.ModelState);
                }
            }

            return View(model);
        }
    }
}
